package com.dy.course.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.dy.course.common.BaseException;
import com.dy.course.common.BaseService;
import com.dy.course.common.Constant;
import com.dy.course.entity.GroupsInfo;
import com.dy.course.mapper.GroupUserMapper;
import com.dy.course.mapper.GroupsMapper;
import com.dy.course.model.GroupUser;
import com.dy.course.model.Groups;
import com.dy.course.model.User;
import com.dy.course.service.IGroupsService;
import com.dy.course.util.DateUtil;

import tk.mybatis.mapper.entity.Example;
@Service
public class GroupsService extends BaseService<Groups> implements IGroupsService {
	
	@Autowired
	private GroupsMapper groupMapper;
	@Autowired
	private GroupUserMapper groupUserMapper;

	@Override
	public void insertGroup(GroupsInfo gb) {
		logger.info("【创建群组服务】参数信息:" + JSON.toJSONString(gb));
		Groups groups = new Groups();
		BeanUtils.copyProperties(gb, groups);
		logger.info("插入群组信息,参数信息:" + JSON.toJSONString(groups));
		groups.setCreateTime(DateUtil.now());
		int count =getMapper().insertSelective(groups);
		if (count < 1) {
			logger.error("【】数据库异常,插入群组列表失败，参数={}" + JSON.toJSONString(groups));
			throw new BaseException(Constant.Code.INSERT_ERROR, "数据库异常", null);
		}
		
		List<Long> list = gb.getGroupUserList();
		list.add(gb.getCreatorId());
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("list", list);
		params.put("groupId", groups.getId());
		logger.info("插入群组用户信息，参数={}" + JSON.toJSONString(params));
		count = groupMapper.insertGroupsUser(params);
		if (count < 1) {
			logger.error("【】数据库异常,插入群组用户失败："+params);
			throw new BaseException(Constant.Code.INSERT_ERROR, "数据库异常", null);
		}
	}
	
	
	/**
	 * 删除群组
	 */
	@Override
	public void deleteGroup(GroupsInfo gb) {
		logger.info("【删除群组服务】,参数信息:gb=" + JSON.toJSONString(gb));
		Groups group = groupMapper.selectByPrimaryKey(gb.groupId);
		if(group == null){
			logger.error("【】数据库异常,数据已经被删除或者不存在,group={}",gb.groupId);
			throw new BaseException(Constant.Code.DELETE_ERROR, "数据库异常,数据被删除或者不存在", null);
		}
		int count = groupMapper.deleteByPrimaryKey(gb.groupId);
		if(count < 1){
			logger.error("【】删除具体群组失败,group={}",JSON.toJSONString(group));
			throw new BaseException(Constant.Code.DELETE_ERROR, "数据库异常,数据被删除或者不存在", null);
		}
		
		//删除群组的用户信息
		Example example = new Example(GroupUser.class);
		example.createCriteria().andEqualTo("groupId", gb.groupId);
		count = groupUserMapper.deleteByExample(example);	
		if(count < 1){
			logger.error("【】删除具体群组失败,group={}",JSON.toJSONString(group));
			throw new BaseException(Constant.Code.DELETE_ERROR, "数据库异常,数据被删除或者不存在", null);
		}
	}

	/**
	 * 修改群组
	 */
	@Override
	public void updateGroup(GroupsInfo gb) {
		logger.info("【修改群组服务】,参数信息:gb=" + JSON.toJSONString(gb));
		Groups groups = groupMapper.selectByPrimaryKey(gb);
		if(groups == null){
			logger.error("【】修改具体群组失败,group={}",JSON.toJSONString(groups));
			throw new BaseException(Constant.Code.SELECT_ERROR, "数据库异常,数据被删除或者不存在", null);
		}
		Example example = new Example(Groups.class);
		example.createCriteria().andEqualTo("id", gb.getId());
		BeanUtils.copyProperties(gb, groups);
		int count = groupMapper.updateByExampleSelective(groups, example);	
		if(count < 1){
			logger.error("【】修改具体群组失败,group={}",JSON.toJSONString(groups));
			throw new BaseException(Constant.Code.UPDATE_ERROR, "数据库异常,数据被删除或者不存在", null);
		}
	}

	@Override
	public List<GroupsInfo> selectGroup(GroupsInfo gb) {
		logger.info("【查询群组列表信息】,参数信息:mb=" + JSON.toJSONString(gb));
		List<GroupsInfo> list = groupMapper.selectGroups(gb);
		if(list.size() == 0){
			logger.error("【】数据库异常,数据不存在或者完整",list.size());
			throw new BaseException(Constant.Code.SELECT_ERROR, "数据库异常,数据不存在或者不完整", null);
		}
		return list;
	}

	@Override
	public List<User> selectGroupUser(GroupsInfo gb) {
		logger.info("【查询群组用户列表信息】,参数信息:mb=" + JSON.toJSONString(gb));
		List<User> list = groupUserMapper.selectGroupsUser(gb);
		if(list.size() == 0){
			logger.error("【】数据库异常,数据不存在或者完整",list.size());
			throw new BaseException(Constant.Code.SELECT_ERROR, "数据库异常,数据不存在或者不完整", null);
		}
		return list;
	}

	/**
	 * 添加群组列表用户信息
	 */
	@Override
	public void insertGroupUser(GroupsInfo gb) {
		logger.info("【查询群组列表信息】,参数信息:gb=" + JSON.toJSONString(gb));
		Groups groups = groupMapper.selectByPrimaryKey(gb.getGroupId());
		if(groups == null){
			logger.error("【】数据库异常,数据已经被删除或者不存在,group={}",gb.getGroupId());
			throw new BaseException(Constant.Code.DELETE_ERROR, "数据库异常,数据被删除或者不存在", null);
		}
		
		List<Long> list = gb.getGroupUserList();
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("list", list);
		params.put("groupId", groups.getId());
		logger.info("插入群组用户信息，参数={}" + JSON.toJSONString(params));
		int count = groupMapper.insertGroupsUser(params);
		if (count < 1) {
			logger.error("【】数据库异常,插入群组用户失败："+params);
			throw new BaseException(Constant.Code.INSERT_ERROR, "数据库异常", null);
		}
	}
}
